package ua.kpi.JavaTraning5;

import java.util.Scanner;

/**
 * This class includes methods for finding all palindroms in line.
 *
 * @author Vitaliy Vasylenko <apa4i007@gmail.com>
 * @version 1.4
 * @since 2013-05-15
 */
public class Palindrome {

    /**
     * The main method to run the program.
     *
     * @param args - passed arguments
     */
    public static void main(String[] args) {
        System.out.println("Input text here please: ");
        Scanner scanner = new Scanner(System.in);
        String inputLine = scanner.nextLine();
        System.out.println("Result:");
        System.out.println(getAllPalindromes(inputLine));
    }

    /**
     * The method finds and returns all palindromes of the input string.
     *
     * @param inputLine - The string in which you want to find palindromes
     * @return int - The number entered from the console
     */
    public static String getAllPalindromes(String inputLine) {
        String allPalindromes = "";
        for (int i = 1; i < inputLine.length() - 1; i++) {
            allPalindromes = getPreviousPalindrome(inputLine, allPalindromes, i);
        }
        return allPalindromes;
    }

    /**
     * The method finds and returns previous palindromes of the input string.
     *
     * @param inputLine          - The string in which you want to find palindromes
     * @param previousPalindrome - Palindrome from the previous time
     * @param counter            - Counter to control the number of passes
     * @return int - The number entered from the console
     */
    public static String getPreviousPalindrome(String inputLine, String previousPalindrome, int counter) {
        for (int i = counter - 1, j = counter + 1; i >= 0 && j < inputLine.length(); i--, j++) {
            if (inputLine.charAt(i) == inputLine.charAt(j)) {
                previousPalindrome = previousPalindrome + inputLine.subSequence(i, j + 1) + "\n";
            } else {
                break;
            }
        }
        return previousPalindrome;
    }

}